// by Xeno
//#define __DEBUG__
#define THIS_FILE "x_setuphc.sqf"
#include "x_setup.sqf"

__TRACE("Running HC setup");

player removeAllEventHandlers "handleDamage";
player removeAllEventHandlers "respawn";
player addEventHandler ["handleDamage", {0}];
player setPos [-10000,10000,0.3];
player enableSimulation false;
enableEnvironment false;
player addEventHandler ["respawn", {
	player setPos [-10000,10000,0.3];
	player removeAllEventHandlers "handleDamage";
	player addEventHandler ["handleDamage", {0}];
	player allowDamage false;
	[QGVAR(hidehc)] call FUNC(NetCallEventToClients);
	player enableSimulation false;
	[QGVAR(nHCObj), player] call FUNC(NetCallEventCTS);
	[QGVAR(hcunit), player] call FUNC(NetCallEventCTS);
	__TRACE("HC respawned");
}];


// TYRGHEN - Initialization to make sure FSM are running correctly:
GVAR(delvecsmt) = [];
GVAR(delinfsm) = [];
GVAR(respawn_ai_groups) = [];
GVAR(delfirebase_objects) = [];
GVAR(ai_artiller_unit_vecs) = [];
GVAR(house_objects) = [];

deleteVehicle GVAR(client_init_trig);
GVAR(client_init_trig) = nil;

__ccppfln(x_shc\x_shcinit.sqf);

_confmapsize = getNumber(configFile/"CfgWorlds"/worldName/"mapSize");
GVAR(island_center) = [_confmapsize / 2, _confmapsize / 2, 300];

/*_mpos = markerPos QGVAR(island_marker);
_mpos set [2,0];
if (str(_mpos) != "[0,0,0]") then {
	_msize = markerSize QGVAR(island_marker);
	GVAR(island_center) = [_msize select 0, _msize select 1, 300];
};*/
/*GVAR(island_x_max) = (GVAR(island_center) select 0) * 2;
GVAR(island_y_max) = (GVAR(island_center) select 1) * 2;*/
GVAR(island_x_max) = _confmapsize;
GVAR(island_y_max) = _confmapsize;

[QGVAR(docnt), {_this spawn FUNC(docreatenexttarget)}] call FUNC(NetAddEventSTO);
[QGVAR(docountera), {0 spawn FUNC(counterattack)}] call FUNC(NetAddEventSTO);
[QGVAR(dodel1), {
	(_this select 1) execFSM "fsms\DeleteUnits.fsm";
	if (!isNil QGVAR(mines_created) && {count GVAR(mines_created) > 0}) then {
		{
			deleteVehicle _x;
		} forEach GVAR(mines_created);
		GVAR(mines_created) = [];
	};
}] call FUNC(NetAddEventSTO);
[QGVAR(dodel2), {
	diag_log ["Count dodel2",count GVAR(house_objects)];
	GVAR(del_camps_stuff) = (_this select 1) select 2;
	(_this select 1) execFSM "fsms\DeleteEmpty.fsm"}
] call FUNC(NetAddEventSTO);
[QGVAR(hcexecsm), {
	GVAR(x_sm_rem_ar) = [];
	GVAR(x_sm_vec_rem_ar) = [];
	GVAR(sm_resolved) = false;
	GVAR(sm_winner) = 0;
	execVM (_this select 1);
}] call FUNC(NetAddEventSTO);

[QGVAR(dodelintelu), {
	if (!isNull GVAR(intel_unit)) then {
		deleteVehicle GVAR(intel_unit);
		GVAR(intel_unit) = objNull;
	};
}] call FUNC(NetAddEventSTO);

[QGVAR(dodelrspgrps), {
	if (count GVAR(respawn_ai_groups) > 0) then {
		0 spawn {
			scriptName "spawn_respawn_ai_groups";
			__TRACE_1("","d_respawn_ai_groups");
			{
				if (typeName _x == "ARRAY") then {
					private "_rgrp";
					_rgrp = _x select 0;
					__TRACE_1("","_rgrp");
					if (!isNil "_rgrp" && {typeName _rgrp == "GROUP"} && {!isNull _rgrp}) then {
						{
							private "_uni";
							_uni = _x;
							__TRACE_1("","_uni");
							if (!isNil "_uni" && {!isNull _uni}) then {
								private "_v";
								_v = vehicle _uni;
								__TRACE_1("","_v");
								if (_v != _uni && {alive _v}) then {_v setDamage 1};
								if (alive _uni) then {_uni setDamage 1}
							};
						} forEach (units _rgrp);
					};
				};
			} forEach GVAR(respawn_ai_groups);
		};
	};
}] call FUNC(NetAddEventSTO);

[QGVAR(hSetVar), {missionNamespace setVariable [_this select 1, _this select 2]}] call FUNC(NetAddEventSTO);
[QGVAR(smclear), {execFSM "fsms\XClearSidemission.fsm"}] call FUNC(NetAddEventSTO);
[QGVAR(mct), {
	GVAR(current_trigger) = (_this select 1) call FUNC(CreateTrigger);
}] call FUNC(NetAddEventSTO);
[QGVAR(xdelct), {deleteVehicle GVAR(current_trigger)}] call FUNC(NetAddEventSTO);
[QGVAR(defendprio), {
	if (count (__XJIPGetVar(GVAR(priority_position))) > 0) then {
		["specops", 0, "basic", 4, (__XJIPGetVar(GVAR(priority_position))) ,50,true] spawn FUNC(CreateInf);
	};
}] call FUNC(NetAddEventSTO);

[QGVAR(hcready)] call FUNC(NetCallEventCTS);

// TYRGHEN - Create reinf
[QGVAR(reinf), {(_this select 1) spawn FUNC(createreinf)}] call FUNC(NetAddEventSTO);

// TYRGHEN - I need to monitor HC fps to see if the ASR_AI problems have that origin
if (GVAR(IS_HC_CLIENT)) then {
	0 spawn {
		while {true} do {
			sleep 55.235;
			diag_log ["HC Client",time,diag_tickTime,diag_frameno,"FPS:",diag_fps];
		};
	};
};